from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from urllib.parse import quote, unquote
from bs4 import BeautifulSoup


def preprocess_table(html_table):
	html_table = "<table>\n" + html_table + "\n</table>"
	return html_table


def parse_table_with_thead_tbody(html_table):
    soup = BeautifulSoup(html_table, 'html.parser')
    table = soup.find('table')
    
    # 初始化表头和表格数据
    headers = []
    table_data = []
    
    # 解析表头
    thead = table.find('thead')
    if thead:
        for th in thead.find_all('th'):
            headers.append(th.get_text(strip=True))
    
    # 解析表格数据
    tbody = table.find('tbody')
    if tbody:
        for row in tbody.find_all('tr'):
            row_data = []
            for cell in row.find_all('td'):
                row_data.append(cell.get_text(strip=True))
            table_data.append(row_data)
    
    return headers, table_data


def format_table_out(headers, table_data):
	output = "\t".join(headers) + "\n"
	for row in table_data:
	    output += "\t".join(row) + "\n"
	return output


# 创建一个 Firefox 浏览器的驱动对象
driver = webdriver.Chrome()

# 使用浏览器打开指定的网页
query_origi = "鸿沟什么意思"
query_ascii = quote(query_origi) # "%E6%9D%A8%E5%B9%82%E7%9A%84%E8%BA%AB%E9%AB%98%E6%98%AF%E5%A4%9A%E5%B0%91"
driver.get("https://debug.baidu-int.com/g?env=hnb.wisedebug.baidu.com&ie=utf-8&info=2&pd=wise&tn=iphone&word={}".format(query_ascii))

# 等待手动登录
import time
print("wait for loging")
time.sleep(15)


# 切换 iframe
driver.switch_to.frame('power')
element = driver.find_element(By.ID, "__BVID__167___BV_tab_button__")
element.click()
time.sleep(15)
element = driver.find_element(By.CSS_SELECTOR, '.json-key.discoveryarc')
element.click()
time.sleep(10)
element = driver.find_element(By.CSS_SELECTOR, "span[class='json-key discoveryarc.multi_queue_dump']")
element.click()
time.sleep(10)
element = driver.find_element(By.CSS_SELECTOR, "span[class='json-key discoveryarc.multi_queue_dump.Level4_ac_merge_bc']")
element.click()
time.sleep(10)
element = driver.find_element(By.CSS_SELECTOR, "body > div:nth-child(1) > div:nth-child(4) > div:nth-child(1) > div:nth-child(2) > div:nth-child(8) > div:nth-child(1) > div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > div:nth-child(3) > div:nth-child(10) > div:nth-child(1) > div:nth-child(2) > div:nth-child(3) > div:nth-child(12) > div:nth-child(1) > div:nth-child(2) > div:nth-child(3) > div:nth-child(4) > div:nth-child(1) > div:nth-child(2) > div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > p:nth-child(1) > span:nth-child(1)")
element.click()
time.sleep(10)
element = driver.find_element(By.CSS_SELECTOR, "button[class='table-databutton']")
element.click()
time.sleep(10)
table = driver.find_element(By.CSS_SELECTOR, "#tableDiv")
table_html = table.get_attribute('innerHTML')
headers, table_data = parse_table_with_thead_tbody(preprocess_table(table_html))
with open("{}.txt".format(query_origi), "w") as wf:
	wf.write(format_table_out(headers, table_data))

driver.quit()
